﻿using System.Diagnostics;
using System.Modules.Extensions;
using System.Modules.Logging.EventViewer;
using System.Reflection;

namespace System.Modules.ExceptionHanlding
{
    /// <summary>
    /// This class is used to Handle Exceptions.
    /// </summary>
    public static class ExceptionHandler
    {
        /// <summary>
        /// This method is an extension for Exception Type where it handles the attached exception.
        /// </summary>
        /// <param name="exception">Exception</param>
        public static void Handle(this Exception exception)
        {
            if (exception != null)
            {
                var source = exception.Source.IsNullOrEmpty() ? Assembly.GetExecutingAssembly().GetName().Name : exception.Source;
                EventViewerLogService.LogEvent(source, exception.Message, EventLogEntryType.Error);
                EventViewerLogService.LogEvent(source, exception.StackTrace, EventLogEntryType.Error);
                exception.InnerException.Handle();
            }
        }
    }
}
